package com.sso.server.controller;/**
 * Created by dell on 2020/4/18.
 */

import com.sso.server.cache.TokenCache;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.UUID;

/**
 * @ClassName LoginController
 * @Description //TODO
 * @Author ljr
 * @Date 2020/4/18 10:09
 */
@Controller
public class LoginController {

    @RequestMapping("/toLogin")
    public String login(HttpServletRequest request){
        String url=request.getParameter("url");
        request.setAttribute("url",url);
        return "login";
    }

    @RequestMapping(value = "/userLogin",method = RequestMethod.POST)
    public String userLogin(HttpServletRequest request){
        String url=request.getParameter("url");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if (username.equals("123")&&password.equals("123")){
            request.getSession().setAttribute("isLogin",true);
            String token=UUID.randomUUID().toString();
            TokenCache.cacheTokenMap.put(request.getSession().getId(),token);
            return "redirect:"+url+"?token="+token;
        }
        request.setAttribute("result","用户名或密码不正确");
        return "login";
    }

    @RequestMapping("/verifyToken")
    @ResponseBody
    public Boolean verifyToken(HttpServletRequest request,String token){
        String tokenResult = TokenCache.cacheTokenMap.get(request.getSession().getId());
        if (token.equalsIgnoreCase(tokenResult)){
            return true;
        }
        return false;
    }


}
